<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\RegisterRequest;

class RegisterController extends Controller
{

    //注册页面

    public function register()
    {
        return view('register.register');
    }

    //注册处理方法

    public function doregister(RegisterRequest $request)
    {
        //检测验证码是否一致
        if($request->input('vcode') != session('Vcode')){
            //返回
            return back()->with('error','验证码错误');
        }
        //提取数据 进行处理
        $data = $request->only(['email','password','username']);
        $data['password'] = Hash::make($data['password']);
        $data['token'] = str_random(50);


        //执行数据插入
        $id = DB::table('users')->insertGetId($data);
        
        if($id){
            //注册成功 发送激活邮件
            //发送邮件
            $this->sendMail($id,$data['token'],$data['email']);
            //显示成功页面
            return view('register.success');

        }else{
            return back()->with('error','注册失败,请联系管理员');
        }
    }

    public function getIndex()
    {
        return view('register.register');
    }
   

    //生成验证码

    public function getVcode()
    {
        ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidat e");
        header('Content-Type: image/jpeg');
        $builder->output();
    }

    /**
     * 发送邮件
     */
    public function sendMail($id,$token,$email)
    {   
        // $str = '优购网欢迎您注册，请点击链接以跳转激活页面<a href="http://www.aaa.com/jihuo?id=?token=?">点击邮件进行激活</a>';
        // Mail::raw('优购网欢迎你注册请点击....', function ($message){
        //     //设置邮件标题
        //     $message->subject('激活邮件');
        //     //发送给谁
        //     $message->to('18031679678@163.com');
        // });
        // echo '2222';
        Mail::send('email.welcome',['id'=>$id,'token'=>$token], function($message)use($email){
            $message->to($email)->subject('激活邮件');
        });

        
    }
    //激活
    public function jihuo(Request $request)
    {
        //接收id
        $id = $request->input('id');
        //查询数据库
        $user = DB::table('users')->where('id',$id)->first();
        //判断
        if($user && $user->token == $request->input('token')){
            //激活用户
            $res = DB::table('users')->where('id',$id)->update(['status'=>2]);
            if($res){
                echo '激活成功';
                return redirect('/')->with('success','激活成功');
            }else{
                echo '激活失败';
            }
        }else{
            echo '非法请求';
        }
    }


   public function getYanzheng()
    {
        $email = $_GET['email'];
        
        $user = DB::table('users')->where('email',$email)->first();
        if($user){
            echo 'true';
        }else{
            echo 'false';
        }

    
    
}


}
  

    